home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / cgazv4n3.zip / EMS.C < prev    next >
Text File  |  1990-02-04  |  7KB  |  182 lines

  1. /********************  EMS.C  ---  Listing 1 *****************
  2. *
  3. *    This listing contains a module of functions that may be called by a C
  4. *    program to manage expanded memory. To compile under Microsoft C, 
  5. *    define the constant MC on the compiler command line (use the /DMC flag).
  6. *    To compile under Turbo C, define TC (-DTC flag).
  7. *
  8. *    Copyright (C) Michael J. Young, 1989.  All rights reserved.
  9. *    May be used freely for non-commercial purposes.
  10. **************************************************************/
  11.  
  12. #include <DOS.H>
  13. #include <STDDEF.H>
  14.  
  15. #include "EMS.H"
  16.  
  17. static int Allocated = 0;          /* Flag indicates EMS memory allocated.  */
  18. static int Handle;                 /* The EMM handle.                       */
  19.  
  20. char far *EmsAlloc (int Pages)
  21. /*
  22.      This function allocates the number of logical EMS pages specified by
  23.      the parameter 'Pages'.  If successful, it returns a far pointer to the
  24.      base of the EMS page frame;  if an error occurs, it returns NULL.
  25. */
  26. {
  27.      char far *FarPtrCh;                /* Temporarily holds return value.  */
  28.      union REGS Reg;
  29.  
  30.      if (!EmsInstalled ())              /* First test if EMM installed.     */
  31.           return (NULL);
  32.  
  33.      if (Allocated)                     /* Make sure that pages have not    */
  34.           return (NULL);                /* already been allocated.          */
  35.  
  36.      Reg.x.bx = Pages;                  /* BX specifies number of pages.    */
  37.      Reg.h.ah = 0x43;                   /* EMM function to allocate pages.  */
  38.      int86 (0x67,&Reg,&Reg);
  39.  
  40.      if (Reg.h.ah)                      /* Test for error.                  */
  41.           return (NULL);                /* NULL return indicates error.     */
  42.  
  43.      Handle = Reg.x.dx;                 /* Store EMM handle from DX.        */
  44.      Allocated = 1;                     /* Set flag to indicate that EMS    */
  45.                                         /* memory has been allocated.       */
  46.  
  47.      Reg.h.ah = 0x41;                   /* EMM function to return segment   */
  48.      int86 (0x67,&Reg,&Reg);            /* address of EMS page frame.       */
  49.  
  50.      if (Reg.h.ah)                      /* Test for error.                  */
  51.           return (NULL);                /* NULL return indicates an error.  */
  52.  
  53. #ifdef MC
  54.      FP_SEG (FarPtrCh) = Reg.x.bx;      /* Segment address is in BX.        */
  55.      FP_OFF (FarPtrCh) = 0;
  56. #endif
  57. #ifdef TC
  58.      FarPtrCh = MK_FP (Reg.x.bx,0);
  59. #endif
  60.  
  61.      return (FarPtrCh);
  62. }    /* end EmsAlloc */
  63.  
  64. int EmsFree (void)
  65. /*
  66.      This function frees all pages of EMS memory that have been allocated by
  67.      a prior call to 'EmsAlloc'.  If successful, it returns zero;  if an error
  68.      occurs, it returns -1.
  69. */
  70. {
  71.      union REGS Reg;
  72.  
  73.      if (!EmsInstalled ())              /* First test if EMM installed.     */
  74.           return -1;
  75.  
  76.      if (!Allocated)                    /* Test that EMS memory has been    */
  77.           return -1;                    /* successfully allocated.          */
  78.  
  79.      Reg.x.dx = Handle;                 /* EMM handle goes in DX.           */
  80.      Reg.h.ah = 0x45;                   /* EMM function to free pages.      */
  81.      int86 (0x67,&Reg,&Reg);
  82.  
  83.      if (Reg.h.ah)                      /* Test for error.                  */
  84.           return (-1);                  /* Return error code.               */
  85.  
  86.      Allocated = 0;                     /* Function successful.             */
  87.      return (0);
  88.  
  89. }    /* end EmsFree */
  90.  
  91. int EmsInstalled (void)
  92. /*
  93.      This function returns a non-zero value if the expanded memory manager
  94.      is installed, or zero if it is not installed.
  95. */
  96. {
  97.      static char *EmmName = "EMMXXXX0"; /* EMM code in device name field.   */
  98.      char far *EmmPtr;                  /* Pointer to device header.        */
  99.  
  100. #ifdef MC
  101.      EmmPtr = (char far *)_dos_getvect (0x67);  /* Address of device header.*/
  102. #endif
  103. #ifdef TC
  104.      EmmPtr = (char far *)getvect (0x67);
  105. #endif
  106.  
  107.      FP_OFF (EmmPtr) = 0x000a;                  /* Adjust offset to name.   */
  108.  
  109.      while (*EmmName)                   /* Test if name field matches the   */
  110.           if (*EmmName++ != *EmmPtr++)  /* EMM code.                        */
  111.                return (0);              /* Mismatch:  return false.         */
  112.  
  113.      return (1);                        /* All characters matched.          */
  114.  
  115. }    /* end EmsInstalled */
  116.  
  117. int EmsMap (int Page0,int Page1,int Page2,int Page3)
  118. /*
  119.      This function maps the four logical pages specified by the parameters
  120.      onto the four physical pages. If a parameter has the value -1,
  121.      the mapping of the corresponding page is left unaltered. If successful,
  122.      the function returns zero; if an error occurs, it returns -1.
  123. */
  124. {
  125.      register int i;
  126.      register int Page;
  127.      union REGS Reg;
  128.  
  129.      if (!EmsInstalled ())              /* First test if EMM installed.     */
  130.           return (-1);
  131.  
  132.      if (!Allocated)                    /* Test that EMS memory has been    */
  133.           return (-1);                  /* successfully allocated.          */
  134.  
  135.      for (i = 0; i < 4; ++i)            /* Four pages to map.               */
  136.           {
  137.           if (i == 0)                   /* Select one parameter with each   */
  138.                Page = Page0;            /* iteration of the loop.           */
  139.           else if (i == 1)
  140.                Page = Page1;
  141.           else if (i == 2)
  142.                Page = Page2;
  143.           else if (i == 3)
  144.                Page = Page3;
  145.           if (Page == -1)
  146.                continue;
  147.  
  148.           Reg.x.bx = Page;              /* Logical page number.             */
  149.           Reg.x.dx = Handle;            /* EMM handle.                      */
  150.           Reg.h.al = (unsigned char) i; /* Physical page number.            */
  151.           Reg.h.ah = 0x44;              /* EMM function to map logical      */
  152.           int86 (0x67,&Reg,&Reg);       /* pages onto physical pages.       */
  153.  
  154.           if (Reg.h.ah)                 /* Test for error.                  */
  155.                return (-1);             /* Return actual error code.        */
  156.           }
  157.  
  158.      return (0);
  159.  
  160. } /* end EmsMap */
  161.  
  162. int EmsPagesAvail (void)
  163. /*
  164.      This function returns the number of free logical pages of EMS memory
  165.      that can be allocated.  If an error occurs, it returns -1.
  166. */
  167. {
  168.      union REGS Reg;
  169.  
  170.      if (!EmsInstalled ())              /* First test if EMM installed.     */
  171.           return (-1);
  172.  
  173.      Reg.h.ah = 0x42;                   /* EMM function to return number of */
  174.      int86 (0x67,&Reg,&Reg);            /* free pages.                      */
  175.  
  176.      if (Reg.h.ah)                      /* Test for error.                  */
  177.           return (-1);                  /* -1 return indicates an error.    */
  178.  
  179.      return (Reg.x.bx);                 /* BX contains the number of pages. */
  180.  
  181. } /* end EmsPagesAvail */
  182.